<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/6
 * Time: 14:08
 */

namespace app\admin\controller;


use think\Db;

class Commongoods extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $keyword = input('keyword');
            $goods_status = input('goods_status');
            $goods_tuijian = input('goods_tuijian');
            $goods_type = input('goods_type');
            
            $where['goods_status'] = ['NEQ', 3];
            $where['goods_type'] = 1;
            if(isset($keyword) && $keyword != ''){
                $where['goods_id|goods_user_id|goods_name'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($goods_status) && $goods_status != ''){
                $where['goods_status'] = $goods_status;
            }
            if(isset($goods_tuijian) && $goods_tuijian != ''){
                $where['goods_tuijian'] = $goods_tuijian;
            }
            if(isset($goods_type) && $goods_type != ''){
                $where['goods_cate'] = $goods_type;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods')
                ->join('up_goods_type', 'gt_id = goods_cate', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods')
                ->join('up_goods_type', 'gt_id = goods_cate', 'left')
                ->field('goods_id, goods_name, goods_thumb, goods_user_id, gt_name, goods_status, goods_tuijian, 
                goods_sail_num, goods_hot_sort, goods_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('goods_hot_sort asc, goods_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $goods_type = Db::table('up_goods_type')
            ->field('gt_id, gt_name')
            ->where('gt_del', 1)
            ->select();
        $this->assign('goods_type', $goods_type);
        return $this->fetch();
    }
    
    /**
     * 评价列表
     * @return mixed|\think\response\Json
     */
    public function goodsComment(){
        if(request()->isAjax ()){
            $gc_type = input('gc_type'); // 1 好评   2 差评
        
            $where['gc_goods_id'] = session('goods_id');
            if(isset($gc_type) && $gc_type != ''){
                $where['gc_type'] = $gc_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods_comment')
                ->join('up_u_user', 'gc_user_id = user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods_comment')
                ->join('up_u_user', 'gc_user_id = user_id', 'LEFT')
                ->field('gc_id, user_username, gc_type, gc_star, gc_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('gc_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $goods_id = input('goods_id');
        if(isset($goods_id) && $goods_id != ''){
            session('goods_id', $goods_id);
        }
        return $this->fetch('commongoods/goodscomment');
    }
    
    /**
     * 评价详情
     * @return mixed
     */
    public function commentDetail(){
        $gc_id = input('gc_id');
        $detail = Db::table('up_goods_comment')
            ->join('up_goods_spec', 'gc_spec_id = spec_id', 'LEFT')
            ->join('up_u_user', 'gc_user_id = user_id', 'LEFT')
            ->where('gc_id', $gc_id)
            ->field('user_username, spec_name, spec_can, gc_star, gc_type, gc_content, gc_images, gc_time')
            ->find();
        $images = trim($detail['gc_images'], ',');
        if(!empty($images)){
            $detail['gc_images'] = explode(',', $images);
        }else{
            $detail['gc_images'] = [];
        }
        return $this->fetch('commongoods/commentdetail', ['detail' => $detail]);
    }
    
    /**
     * 商品规格
     * @return mixed|\think\response\Json
     */
    public function goodsSpec(){
        if(request()->isAjax ()){
            $where['spec_goods_id'] = session('goods_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods_spec')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods_spec')
                ->field('spec_name, spec_price, spec_old_price, spec_can, spec_stock, spec_sail_num')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $goods_id = input('goods_id');
        if(isset($goods_id) && $goods_id != ''){
            session('goods_id', $goods_id);
        }
        return $this->fetch('commongoods/goodsspec');
    }
    
    /**
     * 设置推荐
     * @return \think\response\Json
     */
    public function goodsRecommend(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_tuijian', 1);
        if($res){
            return json(['code' => 200, 'msg' => '设置成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function goodsUnRecommend(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_tuijian', 2);
        if($res){
            return json(['code' => 200, 'msg' => '设置成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function goodsUp(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '设置成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function goodsDown(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '设置成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 排序
     * @return \think\response\Json
     */
    public function updateSort(){
        $goods_id = input('id');
        $field = input('field');
        $value = input('value');
        $res = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->update([
                $field => $value,
                'goods_update_time' => date('Y-m-d H:i:s')
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['code' => 100, 'msg' => '编辑失败']);
        }
    }
}